#!/usr/bin/env python3

# This script uploads the contents of the cache from primary storage to a Redis
# secondary storage.

import redis
import os

config = os.getenv("REDIS_CONF", "localhost")
if ":" in config:
    host, port = config.rsplit(":", 1)
    sock = None
elif config.startswith("/"):
    host, port, sock = None, None, config
else:
    host, port, sock = config, 6379, None
username = os.getenv("REDIS_USERNAME")
password = os.getenv("REDIS_PASSWORD")
context = redis.Redis(
    host=host, port=port, unix_socket_path=sock, password=password
)

CCACHE_MANIFEST = b"cCmF"
CCACHE_RESULT = b"cCrS"

ccache = os.getenv("CCACHE_DIR", os.path.expanduser("~/.cache/ccache"))
filelist = []
for dirpath, dirnames, filenames in os.walk(ccache):
    # sort by modification time, most recently used last
    for filename in filenames:
        if filename.endswith(".lock"):
            continue
        stat = os.stat(os.path.join(dirpath, filename))
        filelist.append((stat.st_mtime, dirpath, filename))
filelist.sort()
files = result = manifest = objects = 0
for mtime, dirpath, filename in filelist:
    dirname = dirpath.replace(ccache + os.path.sep, "")
    if dirname == "tmp":
        continue
    elif filename == "CACHEDIR.TAG" or filename == "stats":
        # ignore these
        files = files + 1
    else:
        (base, ext) = filename[:-1], filename[-1:]
        if ext == "R" or ext == "M":
            if ext == "R":
                result = result + 1
            if ext == "M":
                manifest = manifest + 1
            key = "ccache:" + "".join(list(os.path.split(dirname)) + [base])
            val = open(os.path.join(dirpath, filename), "rb").read() or None
            if val:
                print("%s: %s %d" % (key, ext, len(val)))
                magic = val[0:4]
                if ext == "M":
                    assert magic == CCACHE_MANIFEST
                if ext == "R":
                    assert magic == CCACHE_RESULT
                context.set(key, val)
                objects = objects + 1
        files = files + 1
print(
    "%d files, %d result (%d manifest) = %d objects"
    % (files, result, manifest, objects)
)
